import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: '登录',
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const adminRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Dashboard',
    meta: {
      title: '首页',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'analysis',
        component: () => import('@/views/Dashboard/Admin.vue'),
        name: 'Analysis',
        meta: {
          title: '看板',
          noCache: true,
          affix: true
        }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/sys-menu',
    name: 'System',
    meta: {
      title: '系统管理',
      icon: 'solar:settings-outline',
      alwaysShow: true
    },
    children: [
      {
        path: 'app-parameter-name',
        component: () => import('@/views/Settings/AppParameterName/AppParameterName.vue'),
        name: 'AppParameterName',
        meta: {
          title: '参数模板',
          icon: 'cib:telegram-plane',
          noCache: true
        }
      },
      {
        path: 'app-parameter-name-add',
        component: () => import('@/views/Settings/AppParameterName/AppParameterNameAdd.vue'),
        name: 'AppParameterNameAdd',
        meta: {
          title: '添加模板',
          hidden: true
        }
      },
      {
        path: 'app-parameter-name-edit/id/:id',
        component: () => import('@/views/Settings/AppParameterName/AppParameterNameAdd.vue'),
        name: 'AppParameterNameEdit',
        meta: {
          title: '编辑模板',
          hidden: true
        }
      },
      {
        path: 'sys-menu',
        component: () => import('@/views/SysMenu/SysMenu.vue'),
        name: 'SysMenu',
        meta: {
          title: '菜单管理',
          icon: 'cib:telegram-plane',
          noCache: true
        }
      },
      {
        path: 'sys-menu-add',
        component: () => import('@/views/SysMenu/SysMenuAdd.vue'),
        name: 'SysMenuAdd',
        meta: {
          title: '添加菜单',
          hidden: true
        }
      },
      {
        path: 'sys-menu-edit/id/:id',
        component: () => import('@/views/SysMenu/SysMenuAdd.vue'),
        name: 'SysMenuEdit',
        meta: {
          title: '编辑菜单',
          hidden: true
        }
      },
      {
        path: 'hotel',
        component: () => import('@/views/Settings/Hotel/Hotel.vue'),
        name: 'Hotel',
        meta: {
          title: '主体列表',
          icon: 'cib:telegram-plane'
        }
      },
      {
        path: 'hotel-add',
        component: () => import('@/views/Settings/Hotel/HotelAdd.vue'),
        name: 'HotelAdd',
        meta: {
          title: '添加主体',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      },
      {
        path: 'hotel-edit/id/:id',
        component: () => import('@/views/Settings/Hotel/HotelAdd.vue'),
        name: 'HotelEdit',
        meta: {
          title: '编辑主体',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    name: 'User',
    meta: {
      title: '用户管理',
      icon: 'ph:user-gear-light'
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/Settings/User/User.vue'),
        name: 'UserIndex',
        meta: {
          title: '用户管理',
          icon: 'heroicons:user-group-solid'
        }
      },
      {
        path: 'user-add',
        component: () => import('@/views/Settings/User/UserAdd.vue'),
        name: 'UserAdd',
        meta: {
          title: '添加用户',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      },
      {
        path: 'user-edit/id/:id',
        component: () => import('@/views/Settings/User/UserAdd.vue'),
        name: 'UserEdit',
        meta: {
          title: '编辑用户',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      }
    ]
  },
  {
    path: '/geography',
    component: Layout,
    name: 'Geography',
    redirect: '/geography/region',
    meta: {
      title: '地理数据',
      icon: 'ion:earth',
      alwaysShow: true
    },
    children: [
      {
        path: 'region',
        component: () => import('@/views/Settings/Continent/Continent.vue'),
        name: 'Continent',
        meta: {
          title: '大洲',
          icon: 'cib:telegram-plane'
        }
      },
      {
        path: 'region-add',
        component: () => import('@/views/Settings/Continent/ContinentAdd.vue'),
        name: 'RegionAdd',
        meta: {
          title: '添加大洲',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      },
      {
        path: 'region-edit/id/:id',
        component: () => import('@/views/Settings/Continent/ContinentAdd.vue'),
        name: 'RegionEdit',
        meta: {
          title: '编辑大洲',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      },
      {
        path: 'country',
        component: () => import('@/views/Settings/Country/Country.vue'),
        name: 'Country',
        meta: {
          title: '国家及地区',
          icon: 'cib:telegram-plane'
        }
      },
      {
        path: 'country-add',
        component: () => import('@/views/Settings/Country/CountryAdd.vue'),
        name: 'CountryAdd',
        meta: {
          title: '添加国家',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      },
      {
        path: 'country-edit/id/:id',
        component: () => import('@/views/Settings/Country/CountryAdd.vue'),
        name: 'CountryEdit',
        meta: {
          title: '编辑国家',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      },
      {
        path: 'state',
        component: () => import('@/views/Settings/State/State.vue'),
        name: 'State',
        meta: {
          title: '省份',
          icon: 'cib:telegram-plane'
        }
      },
      {
        path: 'state-add',
        component: () => import('@/views/Settings/State/StateAdd.vue'),
        name: 'StateAdd',
        meta: {
          title: '添加省份',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      },
      {
        path: 'state-edit/id/:id',
        component: () => import('@/views/Settings/State/StateAdd.vue'),
        name: 'StateEdit',
        meta: {
          title: '编辑省份',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      }
    ]
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Dashboard',
    meta: {
      title: '首页',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'analysis',
        component: () => import('@/views/Dashboard/Analysis.vue'),
        name: 'Analysis',
        meta: {
          title: '分析页',
          noCache: true,
          affix: true
        }
      }
    ]
  },
  {
    path: '/settings',
    component: Layout,
    name: 'Settings',
    redirect: '/settings/room-type',
    meta: {
      title: '系统设置',
      icon: 'bx:bxs-component',
      alwaysShow: true
    },
    children: [
      {
        path: 'room-type',
        component: () => import('@/views/Settings/RoomType/RoomType.vue'),
        name: 'RoomType',
        meta: {
          title: '房间类型',
          icon: 'cib:telegram-plane'
        }
      },
      {
        path: 'room-type-add',
        component: () => import('@/views/Settings/RoomType/RoomTypeAdd.vue'),
        name: 'RoomTypeAdd',
        meta: {
          title: '添加房间类型',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      },
      {
        path: 'room-type-edit/id/:id',
        component: () => import('@/views/Settings/RoomType/RoomTypeAdd.vue'),
        name: 'RoomTypeEdit',
        meta: {
          title: '编辑房间类型',
          icon: 'cib:telegram-plane',
          hidden: true,
          canTo: true
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
